import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'dart:ui';
import 'dart:math';

class DraggablePage extends StatelessWidget {
  var drag_item_height = 80.0;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Scaffold(
        appBar: AppBar(
          title: Text("Draggable"),
          centerTitle: true,
        ),
        body: Stack(
          children: <Widget>[
            DragItem(dragModel: DragModel(Offset(0.0, 0.0), '红', Colors.red)),
            DragItem(
                dragModel: DragModel(
                    Offset(0.0, 1 * drag_item_height), '橙', Colors.orange)),
            DragItem(
                dragModel: DragModel(
                    Offset(0.0, 2 * drag_item_height), '黄', Colors.yellow)),
            DragItem(
                dragModel: DragModel(
                    Offset(0.0, 3 * drag_item_height), '绿', Colors.green)),
            DragItem(
                dragModel: DragModel(Offset(0.0, 4 * drag_item_height), '青',
                    Colors.indigoAccent)),
            DragItem(
                dragModel: DragModel(
                    Offset(0.0, 5 * drag_item_height), '蓝', Colors.blue)),
            DragItem(
                dragModel: DragModel(
                    Offset(0.0, 6 * drag_item_height), '紫', Colors.purple)),
            DragDestination(),
          ],
        ),
      ),
    );
  }
}

class DragItem extends StatefulWidget {
  DragModel dragModel;

  DragItem({Key? key, required this.dragModel}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _DragItemState();
  }
}

class _DragItemState extends State<DragItem> {
  var physicalSize = window.physicalSize;
  double drag_item_height = 80.0;
  double appBarHeight = kToolbarHeight;
  double statusBarHeight = MediaQueryData.fromWindow(window).padding.top;
  late double screenWidth;

  late double screenHeight;
  late Offset pos;

  @override
  void initState() {
    super.initState();
    pos = Offset(widget.dragModel.offset.dx, widget.dragModel.offset.dy);
    screenWidth = physicalSize.width;
    screenHeight = physicalSize.height;
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
        left: pos.dx,
        top: pos.dy,
        child: Draggable(
          data: widget.dragModel,
          child: Container(
            width: drag_item_height,
            height: drag_item_height,
            color: widget.dragModel.itemBgColor,
            child: Center(
              child: Text(
                widget.dragModel.itemName,
                style: TextStyle(
                  color: Colors.white,
                  decoration: TextDecoration.none,
                  fontSize: 12.0,
                ),
              ),
            ),
          ),
          onDraggableCanceled: (velocity, offset) {
            if (!mounted) return;
            setState(() {
              //坐标是根据全屏算的，需要计算appbar和statusBar的高度
              print(offset);
              double dx = offset.dx;
              double dy = offset.dy - appBarHeight - statusBarHeight;

              //临界点判断
              if (dx < 0) {
                dx = 0.0;
              }

              if (dx > screenWidth - drag_item_height) {
                dx = screenWidth - drag_item_height;
              }

              if (dy < 0) {
                dy = 0.0;
              }

              if (offset.dy + drag_item_height > screenHeight) {
                dy = screenHeight -
                    drag_item_height -
                    appBarHeight -
                    statusBarHeight;
              }

              pos = new Offset(dx, dy);
//              pos = new Offset(widget.dragModel.offset.dx, widget.dragModel.offset.dy);
            });
          },
          feedback: Container(
              width: drag_item_height,
              height: drag_item_height,
              color: widget.dragModel.itemBgColor.withOpacity(0.5),
              child: Center(
                child: Text(
                  widget.dragModel.itemName,
                  style: TextStyle(
                    color: Colors.white,
                    decoration: TextDecoration.none,
                    fontSize: 14.0,
                  ),
                ),
              )),
        ));
  }
}

class DragDestination extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _DragDestinationState();
  }
}

class _DragDestinationState extends State<DragDestination> {
  int _score = 0;
  late Color _curRandomColor;
  List<Color> colorList = [
    Colors.red,
    Colors.orange,
    Colors.yellow,
    Colors.green,
    Colors.indigoAccent,
    Colors.blue,
    Colors.purple
  ];

  @override
  void initState() {
    super.initState();
    _curRandomColor = colorList[Random().nextInt(6)];
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      right: 0.0,
      child: DragTarget(
        onAccept: (DragModel model) {
          if (model.itemBgColor == _curRandomColor) {
            _score++;
          }
          setState(() {
            _curRandomColor = colorList[Random().nextInt(7)];
          });
        },
        builder: (
          BuildContext context,
          List<dynamic> accepted,
          List<dynamic> rejected,
        ) {

          return Container(
            width: 200.0,
            height: 200.0,
            decoration: BoxDecoration(
              color: _curRandomColor,
            ),
            child: Center(
              child: Text(
                "放入正确的颜色\n您的得分：$_score",
                style: new TextStyle(fontSize: 16.0, color: Colors.white),
              ),
            ),
          );
        },
      ),
    );
  }
}

class DragModel {
  Offset offset;
  String itemName;
  Color itemBgColor;

  DragModel(this.offset, this.itemName, this.itemBgColor);
}
